package view;

import java.awt.EventQueue;
import java.awt.FileDialog;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.FlowLayout;

import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.WindowConstants;

import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.EventListener;

import javax.swing.JFormattedTextField;
import java.awt.Color;

@SuppressWarnings("serial")
public class CueCreator extends JFrame {

	@SuppressWarnings("unused")
	private EventListener controller;
	
	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_2;
	private JPanel panel_3;
	private JLabel lblCueLabel;
	private JTextField labelTextField;
	private JLabel lblType;
	private JComboBox<String> mscTypecomboBox;
	private JButton btnNewButton;
	private JLabel lblJmidiCue;
	private JFormattedTextField jCueTextField_1;
	private JLabel lblMscCue;
	private JFormattedTextField mscCueTextField_2;
	private JPanel panel_1;
	private JPanel panel_4;
	private JLabel lblNewLabel_1;
	private JComboBox<String> mscCommandComboBox_2;
	@SuppressWarnings("unused")
	private JLabel lblFilePath;
	private JTextField filePathTextField;
	private JButton filePathJButton;
	private JLabel lblPreWait;
	private JFormattedTextField formattedTextField;
	private JLabel lblPostWait;
	private JFormattedTextField formattedTextField_1;
	private JLabel label;
	private JComboBox<String> comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					CueCreator frame = new CueCreator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public CueCreator()
	{
		new CueCreator(null);
	}
	/**
	 * Create the frame.
	 * @param controller
	 */
	public CueCreator(EventListener controller) {
		this.controller = controller;
		this.controller = controller;
		setTitle("Cue Creator");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		//setResizable(false);
		setBounds(100, 100, 450, 180);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0};
		gbl_contentPane.rowHeights = new int[]{0 ,0 ,0 ,0 ,0};
		gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		contentPane.setLayout(gbl_contentPane);
		
		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.anchor = GridBagConstraints.NORTH;
		gbc_panel.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		contentPane.add(panel, gbc_panel);
		panel.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 1));
		
		lblCueLabel = new JLabel("Label:");
		panel.add(lblCueLabel);
		
		labelTextField = new JTextField();
		panel.add(labelTextField);
		labelTextField.setColumns(10);
		
		label = new JLabel("Cue Type:");
		label.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(label);
		
		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"MSC", "Audio"}));
		comboBox.setToolTipText("Cue Type");
		comboBox.setActionCommand("Cue Type");
		comboBox.addActionListener((ActionListener) controller);
		panel.add(comboBox);
		
		lblJmidiCue = new JLabel("Cue #:");
		panel.add(lblJmidiCue);
		
		jCueTextField_1 = new JFormattedTextField();
		jCueTextField_1.setColumns(3);
		panel.add(jCueTextField_1);
		
		panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.anchor = GridBagConstraints.NORTH;
		gbc_panel_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 1;
		contentPane.add(panel_1, gbc_panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 1));
		
		lblPreWait = new JLabel("Pre Wait:");
		panel_1.add(lblPreWait);
		
		formattedTextField = new JFormattedTextField(new SimpleDateFormat("HH:mm:ss.SS"));
		formattedTextField.addFocusListener((FocusListener) controller);
		formattedTextField.setForeground(Color.LIGHT_GRAY);
		formattedTextField.setToolTipText("HH:mm:SS.ss");
		formattedTextField.setText("00:00:00.00");
		formattedTextField.setColumns(8);
		panel_1.add(formattedTextField);
		
		lblPostWait = new JLabel("Post Wait:");
		panel_1.add(lblPostWait);
		
		formattedTextField_1 = new JFormattedTextField(new SimpleDateFormat("HH:mm:ss.SS"));
		formattedTextField_1.setForeground(Color.LIGHT_GRAY);
		formattedTextField_1.setText("00:00:00.00");
		formattedTextField_1.setToolTipText("HH:mm:SS.ss");
		formattedTextField_1.setColumns(8);
		panel_1.add(formattedTextField_1);
		
		panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.anchor = GridBagConstraints.NORTH;
		gbc_panel_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 2;
		contentPane.add(panel_2, gbc_panel_2);
		panel_2.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 1));
		
		lblType = new JLabel("Type:");
		panel_2.add(lblType);
		
		mscTypecomboBox = new JComboBox<String>();
		mscTypecomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Lighting - ETC Express"}));
		mscTypecomboBox.setToolTipText("Type");
		panel_2.add(mscTypecomboBox);
		
		lblMscCue = new JLabel("MSC Cue #:");
		panel_2.add(lblMscCue);
		
		mscCueTextField_2 = new JFormattedTextField(NumberFormat.getInstance());
		panel_2.add(mscCueTextField_2);
		mscCueTextField_2.setColumns(3);
		
		panel_4 = new JPanel();
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.anchor = GridBagConstraints.NORTH;
		gbc_panel_4.gridheight = 1;
		gbc_panel_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_panel_4.gridx = 0;
		gbc_panel_4.gridy = 3;
		contentPane.add(panel_4, gbc_panel_4);
		panel_4.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 1));
		
		lblNewLabel_1 = new JLabel("MSC Command:");
		panel_4.add(lblNewLabel_1);
		
		mscCommandComboBox_2 = new JComboBox<String>();
		mscCommandComboBox_2.setModel(new DefaultComboBoxModel<String>(new String[] {"GO"}));
		panel_4.add(mscCommandComboBox_2);
		
		panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridheight = 2;
		gbc_panel_3.gridx = 0;
		gbc_panel_3.gridy = 4;
		gbc_panel_3.weighty = .8;
		gbc_panel_3.ipady = 50;
		contentPane.add(panel_3, gbc_panel_3);
		GridBagLayout gbl_panel_3 = new GridBagLayout();
		gbl_panel_3.columnWidths = new int[]{0, 0};
		gbl_panel_3.rowHeights = new int[]{0, 0};
		gbl_panel_3.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_3.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panel_3.setLayout(gbl_panel_3);
		
		btnNewButton = new JButton("OK!");
		btnNewButton.addActionListener((ActionListener) controller);
		btnNewButton.setActionCommand("CueCreator OK!");
		
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.anchor = GridBagConstraints.BELOW_BASELINE;
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 0;
		panel_3.add(btnNewButton, gbc_btnNewButton);
		
		
		//Extra components not added into containers during intialization
		lblFilePath = new JLabel("File Path:");
		filePathTextField = new JTextField();
		filePathTextField.setColumns(20);
		
		filePathJButton = new JButton("...");
		filePathJButton.addActionListener((ActionListener) controller);
		filePathJButton.setActionCommand("CC file browser");
		
	}

	public String[] getFormInfo() {
		
		String[] output = new String[7];
		output[0] = (String) comboBox.getSelectedItem();
		output[1] = labelTextField.getText();
		output[2] = jCueTextField_1.getText();
		output[3] = (String) mscTypecomboBox.getSelectedItem();
		output[4] = mscCueTextField_2.getText();
		output[5] = (String) mscCommandComboBox_2.getSelectedItem();
		output[6] = filePathTextField.getText();
		
		return output;
	}

	public void changeOptions(String string) 
	{
		if(string.equals("Audio"))
		{
			panel_2.removeAll();
			panel_4.removeAll();

			panel_2.add(lblFilePath);
			panel_2.add(filePathTextField);
			panel_2.add(filePathJButton);
			System.out.println("Pie");
			this.invalidate();
			this.validate();
			this.repaint();
			
		}
		else if(string.equals("MSC"))
		{
			panel_2.removeAll();
			panel_4.removeAll();
			
			panel_2.add(lblType);
			panel_2.add(mscTypecomboBox);
			panel_2.add(lblMscCue);
			panel_2.add(mscCueTextField_2);
			
			panel_4.add(lblNewLabel_1);
			panel_4.add(mscCommandComboBox_2);
			
			this.invalidate();
			this.validate();
			this.repaint();
		}
		
	}
	
	public void selectFile()
	{
		
		System.setProperty("apple.awt.fileDialogForDirectories", "true");
		FileDialog fd = new FileDialog(this, "WAV & MP3 Files Only"); 
		//fd.setDirectory(_projectsBaseDir.getPath()); 
		fd.setLocation(50,50);
		fd.setVisible(true); 
		String path = fd.getDirectory() + fd.getFile();
		System.setProperty("apple.awt.fileDialogForDirectories", "false");
		
		if(path.contains(".mp3" ) || path.contains(".wav"))
			filePathTextField.setText(path);
		else
			filePathTextField.setText("");
		
//		JFileChooser chooser = new JFileChooser();
//	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
//	            "WAV & MP3 Audio", "wav", "mp3");
//	        chooser.setFileFilter(filter);
//		int returnVal = chooser.showOpenDialog(this);
//	    if(returnVal == JFileChooser.APPROVE_OPTION) {
//	    	filePathTextField.setText(chooser.getSelectedFile().getPath());
	    //}
	}

	public String getCurrentCueType() {
		return (String) comboBox.getSelectedItem();
	}
	
}
